package auctionServer.utilities;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class UDPConnection {
	
	private DatagramSocket datagramSocket ;
	private byte[] buffer; 
	private InetAddress receiverAddress ;

	public UDPConnection() {
		try {
			this.datagramSocket= new DatagramSocket();
		} catch (SocketException e1) {
			System.out.println("SocketException Error at UDPConnection.UDPConnection().catch1");
		}
		try {
			this.receiverAddress= InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			System.out.println("UnknownHostException Error at UDPConnection.UDPConnection().catch2");
		}
	}
	public void sendMessage(String message,int udpPort){
		buffer=message.getBytes();
		DatagramPacket packet = new DatagramPacket(buffer, buffer.length, receiverAddress, udpPort);
		try {
			datagramSocket.send(packet);
		} catch (IOException e) {
			System.out.println("IOException Error at UDPConnection.sendMessage.catch1");
		}
	}

	public void close(){
		datagramSocket.close();
	}
}
